package com.Txunda.fxw.ui.fgt;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.config.Settings;
import com.Txunda.fxw.ui.activity.home.SmallSizeAty;
import com.Txunda.fxw.ui.activity.other.CollectAty;
import com.Txunda.fxw.ui.activity.other.SettingAty;
import com.Txunda.fxw.ui.activity.other.SingleDetailAty;
import com.Txunda.fxw.ui.base.BaseFgt;
import com.Txunda.fxw.ui.http.Index;
import com.Txunda.fxw.view.banner.BannerAdapter;
import com.Txunda.fxw.view.banner.DotView;
import com.Txunda.fxw.view.banner.SliderBanner;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;

public class HomeFgt extends BaseFgt {
    protected int barHeight;
    @BindView(R.id.home_setting_iv)
    ImageView homeSettingIv;
    @BindView(R.id.tv_collect)
    FrameLayout tvCollect;
    @BindView(R.id.home_viewpager)
    ViewPager homeViewpager;
    @BindView(R.id.home_dotview)
    DotView homeDotview;
    @BindView(R.id.home_banner)
    SliderBanner homeBanner;
    @BindView(R.id.home_title_fragme)
    FrameLayout homeTitleFragme;
    @BindView(R.id.tv_shop)
    TextView tvShop;
    @BindView(R.id.tv_store)
    TextView tvStore;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.tv_homestay)
    TextView tvHomestay;
    @BindView(R.id.iv_banner)
    ImageView ivBanner;
    @BindView(R.id.gcc_iv)
    ImageView gccIv;
    @BindView(R.id.ccxd_iv)
    ImageView ccxdIv;
    @BindView(R.id.sgdz_iv)
    ImageView sgdzIv;
    @BindView(R.id.jxwd_iv)
    ImageView jxwdIv;
    @BindView(R.id.gyzn_iv)
    ImageView gyznIv;
    @BindView(R.id.cxsq_iv)
    ImageView cxsqIv;
    @BindView(R.id.fxsd_iv)
    ImageView fxsdIv;
    @BindView(R.id.xzc_iv)
    ImageView xzcIv;
    @BindView(R.id.obs_scroll_view)
    ObservableScrollView obsScrollView;
    Unbinder unbinder;
    private Index index;
    private Bundle bundle;
    private ArrayList<ImageView> albums;

    private MyViewAdapter adapter;
    private ArrayList<Map<String, String>> advert;
    private ArrayList<Map<String, String>> gvList;
    private String type = "";

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 设置轮播图宽高
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Settings.displayWidth,
                Settings.displayWidth * 2 / 5 + barHeight);
        homeBanner.setLayoutParams(params);
        // 设置轮播图及圆弧所在布局宽高
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(Settings.displayWidth,
                Settings.displayWidth * 2 / 5);
        homeTitleFragme.setLayoutParams(param);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_home;
    }

    @Override
    @OnClick({R.id.tv_shop, R.id.tv_store, R.id.tv_buy, R.id.tv_homestay,
            R.id.tv_collect, R.id.aty_icon_right_img, R.id.iv_banner,
            R.id.gcc_iv, R.id.ccxd_iv, R.id.sgdz_iv, R.id.jxwd_iv, R.id.gyzn_iv,
            R.id.cxsq_iv, R.id.fxsd_iv, R.id.xzc_iv, R.id.home_setting_iv})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.home_setting_iv:
                startActivity(SettingAty.class, null);
                break;
            case R.id.tv_shop:// 网上超市
                bundle = new Bundle();
                bundle.putString("id", "5");
                bundle.putString("title", "");
                bundle.putString("count", "2");
                bundle.putInt("dataType", 1); //6.18号添加上的
                bundle.putString("type", "sc"); //6。19号修改的
                startActivity(SingleDetailAty.class, bundle);
                break;
            case R.id.tv_store:// 实体店
                bundle = new Bundle();
                bundle.putString("id", "3");
                bundle.putString("count", "2");
                bundle.putString("title", "");
                bundle.putInt("dataType", 1); //6.18号添加上的
                bundle.putString("type", "ss");
                startActivity(SingleDetailAty.class, bundle);
                break;
            case R.id.tv_buy:// 团购直销
                bundle = new Bundle();
                bundle.putString("id", "15");
                bundle.putString("count", "2");
                bundle.putString("title", "");
                bundle.putInt("dataType", 1);
                bundle.putString("type", "st");
                startActivity(SingleDetailAty.class, bundle);
                break;
            case R.id.tv_homestay:// 民宿
                bundle = new Bundle();
                bundle.putString("id", "4");
                bundle.putString("count", "2");
                bundle.putString("title", "");
                bundle.putInt("dataType", 1); //6.18号添加上的
                bundle.putString("type", "sm");
                startActivity(SingleDetailAty.class, bundle);
                break;
            case R.id.tv_collect:// 收藏
                startActivity(CollectAty.class, null);
                break;
            case R.id.aty_icon_right_img:// 设置
                startActivity(SettingAty.class, null);
                break;
            case R.id.iv_banner:// 广告图片
//                startActivity(SmallSizedAty.class, null);
                startActivity(SmallSizeAty.class, null);
                break;
            case R.id.gcc_iv:// 0
                type = "s1f1";
                toNewClassfiy(v);
                break;
            case R.id.ccxd_iv:// 1
                type = "s2f1";
                toNewClassfiy(v);
                break;
            case R.id.sgdz_iv:// 2
                type = "s3f1";
                toNewClassfiy(v);
                break;
            case R.id.jxwd_iv:// 3
                type = "s4f1";
                toNewClassfiy(v);
                break;
            case R.id.gyzn_iv:// 4
                type = "s5f1";
                toNewClassfiy(v);
                break;
            case R.id.cxsq_iv://5
                type = "s6f1";
                toNewClassfiy(v);
                break;
            case R.id.fxsd_iv://6
                type = "s7f1";
                toNewClassfiy(v);
                break;
            case R.id.xzc_iv://7
                type = "s8f1";
                toNewClassfiy(v);
                break;
            default:
                break;
        }
    }

    private void toNewClassfiy(View view) {
        String tag = (String) view.getTag();
        if (tag == null || tag.equals(""))
            return;
        for (Map<String, String> map : gvList) {
            if (tag.equals(map.get("name"))) {
                String id = map.get("id");
                bundle = new Bundle();
                bundle.putInt("dataType", 1);
                bundle.putString("id", id);
                bundle.putString("title", "");
                bundle.putString("type", type);
                bundle.putString("count", "2");
                startActivity(SingleDetailAty.class, bundle);
                return;
            }
        }
    }

    @Override
    protected void initialized() {
        index = new Index();
        adapter = new MyViewAdapter();
    }

    @Override
    protected void requestData() {
        index.cate(this);
        index.adlist(this);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("cate")) {
            gvList = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            return;
        }
        if (requestUrl.contains("adlist")) {
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data))
                return;
            advert = JSONUtils.parseKeyAndValueToMapList(data.get("ad"));
            // 初始化轮播图相应数据参数
            albums = new ArrayList<>();
            for (int i = 0; i < advert.size(); i++) {
                ImageView imageView = new ImageView(getActivity());
                imageView.setScaleType(ScaleType.FIT_XY);
                albums.add(imageView);
            }
            homeBanner.setAdapter(adapter);
            homeBanner.setDotNum(ListUtils.getSize(albums));
            homeBanner.beginPlay();
        }
    }

    /**
     * 轮播图的适配器
     */
    private class MyViewAdapter extends BannerAdapter {

        @Override
        public View getView(LayoutInflater layoutInflater, final int position) {
            ImageView imageView = albums.get(position);
            Glide.with(getActivity()).load(advert.get(position).get("ad_pic")).centerCrop()
                    .placeholder(R.drawable.ic_default).dontAnimate().into(imageView);
            if (!TextUtils.isEmpty(advert.get(position).get("ad_url"))) {
                imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Map<String, String> map = advert.get(position);
                        String id = map.get("id");
                        if (!map.get("ad_url").equals("")) {
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "lb");
                            bundle.putString("class_id", id);
                            bundle.putString("com_id", id);
                            bundle.putString("count", "1");
                            bundle.putString("url", map.get("ad_url"));
                            startActivity(WebAty.class, bundle);
                        }
                    }
                });
            }
            return imageView;
        }

        @Override
        public int getCount() {
            return ListUtils.getSize(albums);
        }
    }
}
